package com.gosling.cloudspace.tools;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 校验验签内容
 *
 * @author lihonglin
 * @date 2022/10/20 13:55
 */
public class CheckSignTool {

    public static void main(String[] args) {
        signEx();
    }

    private static void signEx() {
        //        String s = "{\"apiName\":\"ADD_BASE_INFO\",\"apiVersion\":\"1.0.0.0\",\"orgNo\":\"G501367\",\"address\":\"四川省成都市郫都区安德街道南街226号1层\",\"businessLicenceNo\":\"91510124MA7NDTN237\",\"businessLicencePeriodEnd\":\"\",\"businessLicencePeriodStart\":\"2022-04-19\",\"businessScope\":\"保健食品(预包装)销售\",\"businessType\":\"1\",\"cityCode\":6510,\"contactPhone\":18180900081,\"countyCode\":6514,\"icpStatus\":\"03\",\"inviteCode\":\"B1HE41\",\"legalPerson\":\"成云丽\",\"legalPersonCertNum\":\"510103196306136229\",\"legalPersonCertPeriodStart\":\"2006-07-17\",\"legalPersonCertType\":0,\"legalPersonNationality\":\"Chinese\",\"legalPersonSex\":0,\"mccCode\":5398,\"merchantAttribute\":2,\"merchantDomain\":\"yuank.18my.net\",\"merchantProperty\":2,\"merchantType\":1,\"name\":\"成都元源康健生物科技有限公司\",\"phoneNo\":\"\",\"provinceCode\":6500,\"registeredFund\":\"5000000\",\"sendMsgFlag\":\"0\",\"shareholder\":\"成云丽\",\"shareholderCertPeriodStart\":\"2006-07-17\",\"shareholderCertType\":0,\"shareholderIdNum\":\"510103196306136229\",\"shareholderType\":\"0\",\"shortName\":\"成都元源康健生物科技有限公司\",\"merchantNucAttr\":\"02\",\"signMsg\":\"W+pkkuCJ8DVE2iPgC+a7fFlZJJ1KAGReASL078o9za58ulDhQHpB1dzfApHiO1wB71UeNbaeu3H4jsYkO54tJbjMwDVGpwIzP9C2pp7ZxgnANkm4VzWa08zwwusg7kynPfrCh1nbLXqJeRRBV3psBP0gY8iCLKwJHuNkXlE0wBOxzKXLRMhb3wz\\/uHUV8ZCEj3M0jHsfYeWq8hQKrgWXmx+d1F3lKjBrAlzEXS8si7VUSBz4S4WKqHIzvk3pp1oyTJweSsKJyfsGdkE9q0OmNHzw6fqiAXIwIFbpqZNW6xdDEpnsfOw+EbgJBoHLzh\\/CnUE+X2BD+qsaUYcX+mkN3Q==\"}";
//        String s1 = "address=四川省成都市郫都区安德街道南街226号1层&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510124MA7NDTN237&businessLicencePeriodEnd=&businessLicencePeriodStart=2022-04-19&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=成云丽&legalPersonCertNum=510103196306136229&legalPersonCertPeriodStart=2006-07-17&legalPersonCertType=0&legalPersonNationality=Chinese&legalPersonSex=0&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=成都元源康健生物科技有限公司&orgNo=G501367&phoneNo=&provinceCode=6500&registeredFund=5000000&sendMsgFlag=0&shareholder=成云丽&shareholderCertPeriodStart=2006-07-17&shareholderCertType=0&shareholderIdNum=510103196306136229&shareholderType=0&shortName=成都元源康健生物科技有限公司";
//        String s2 = "address=四川省成都市郫都区安德街道南街226号1层&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510124MA7NDTN237&businessLicencePeriodStart=2022-04-19&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=成云丽&legalPersonCertNum=510103196306136229&legalPersonCertPeriodStart=2006-07-17&legalPersonCertType=0&legalPersonNationality=Chinese&legalPersonSex=0&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=成都元源康健生物科技有限公司&orgNo=G501367&provinceCode=6500&registeredFund=5000000&sendMsgFlag=0&shareholder=成云丽&shareholderCertPeriodStart=2006-07-17&shareholderCertType=0&shareholderIdNum=510103196306136229&shareholderType=0&shortName=成都元源康健生物科技有限公司\n";
        String s = "{\"apiName\":\"ADD_BASE_INFO\",\"apiVersion\":\"1.0.0.0\",\"orgNo\":\"G501367\",\"address\":\"成都市金牛区沙湾东二路1号1栋1单元22层25号\",\"businessLicenceNo\":\"91510106350615195B\",\"businessLicencePeriodEnd\":\"\",\"businessLicencePeriodStart\":\"2022-07-22\",\"businessScope\":\"保健食品(预包装)销售\",\"businessType\":1,\"cityCode\":6510,\"contactPhone\":18180900081,\"countyCode\":6514,\"icpStatus\":\"03\",\"inviteCode\":\"B1HE41\",\"legalPerson\":\"罗文正\",\"legalPersonCertNum\":\"511025198302282053\",\"legalPersonCertPeriodStart\":\"2017-11-25\",\"legalPersonCertType\":0,\"legalPersonNationality\":\"Chinese\",\"legalPersonSex\":0,\"mccCode\":5398,\"merchantAttribute\":2,\"merchantDomain\":\"yuank.18my.net\",\"merchantProperty\":2,\"merchantType\":1,\"name\":\"四川盒达信息技术有限公司\",\"phoneNo\":\"\",\"provinceCode\":6500,\"registeredFund\":\"5000000\",\"sendMsgFlag\":0,\"shareholder\":\"罗文正\",\"shareholderCertPeriodStart\":\"2017-11-25\",\"shareholderCertType\":0,\"shareholderIdNum\":\"511025198302282053\",\"shareholderType\":0,\"shortName\":\"四川盒达信息技术有限公司\",\"merchantNucAttr\":\"02\",\"signMsg\":\"IgUpKk\\/+Ll2GLH9NB9\\/SbqdEEIsaEi3mOqdd9JJu\\/TL0IJFQl9dneRNacsaoqDEcJcxZWsUUJ0wOjwv3qBXiGsO7U897o88bhcv3WFU668oN1\\/kyqZ\\/0Oluo8GNRr5uUpVKlx3vMjJZ9igchk5sgjmMXsUu1P4EEC95c56TzuKwUcLjC1hXkrNF4nWuGQXDy9NRJOQuFB2SetnSMSj64JI1qZ6YyvJlk+JbQ7jKaQ6UvBo6zzf6XEaLKrpSi1jnoWYFGZssl5rm5fN6anaEuR\\/tsqUV0jUInbCOdybA4LTpA3RhyJENXlhlPI8NxVYgggOY5Ml7pZVekwawHwGNETQ==\"}";
        String s3 = "address=成都市金牛区沙湾东二路1号1栋1单元22层25号&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510106350615195B&businessLicencePeriodStart=2022-07-22&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=罗文正&legalPersonCertNum=511025198302282053&legalPersonCertPeriodStart=2017-11-25&legalPersonNationality=Chinese&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=四川盒达信息技术有限公司&orgNo=G501367&provinceCode=6500&registeredFund=5000000&shareholder=罗文正&shareholderCertPeriodStart=2017-11-25&shareholderIdNum=511025198302282053&shortName=四川盒达信息技术有限公司";
        String s4 = "address=成都市金牛区沙湾东二路1号1栋1单元22层25号&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510106350615195B&businessLicencePeriodStart=2022-07-22&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=罗文正&legalPersonCertNum=511025198302282053&legalPersonCertPeriodStart=2017-11-25&legalPersonNationality=Chinese&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=四川盒达信息技术有限公司&orgNo=G501367&provinceCode=6500&registeredFund=5000000&shareholder=罗文正&shareholderCertPeriodStart=2017-11-25&shareholderIdNum=511025198302282053&shortName=四川盒达信息技术有限公司\n";
//        String s4 = "address=成都市金牛区沙湾东二路1号1栋1单元22层25号&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510106350615195B&businessLicencePeriodStart=2022-07-22&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=罗文正&legalPersonCertNum=511025198302282053&legalPersonCertPeriodStart=2017-11-25&legalPersonCertType=0&legalPersonNationality=Chinese&legalPersonSex=0&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=四川盒达信息技术有限公司&orgNo=G501367&provinceCode=6500&registeredFund=5000000&sendMsgFlag=0&shareholder=罗文正&shareholderCertPeriodStart=2017-11-25&shareholderCertType=0&shareholderIdNum=511025198302282053&shareholderType=0&shortName=四川盒达信息技术有限公司";
        String s5 = "address=成都市金牛区沙湾东二路1号1栋1单元22层25号&apiName=ADD_BASE_INFO&apiVersion=1.0.0.0&businessLicenceNo=91510106350615195B&businessLicencePeriodStart=2022-07-22&businessScope=保健食品(预包装)销售&businessType=1&cityCode=6510&contactPhone=18180900081&countyCode=6514&icpStatus=03&inviteCode=B1HE41&legalPerson=罗文正&legalPersonCertNum=511025198302282053&legalPersonCertPeriodStart=2017-11-25&legalPersonCertType=0&legalPersonNationality=Chinese&legalPersonSex=0&mccCode=5398&merchantAttribute=2&merchantDomain=yuank.18my.net&merchantNucAttr=02&merchantProperty=2&merchantType=1&name=四川盒达信息技术有限公司&orgNo=G501367&provinceCode=6500&registeredFund=5000000&sendMsgFlag=0&shareholder=罗文正&shareholderCertPeriodStart=2017-11-25&shareholderCertType=0&shareholderIdNum=511025198302282053&shareholderType=0&shortName=四川盒达信息技术有限公司";
        String jsonString = JSONObject.toJSONString("param");
        Map<String, String> map = JSONObject.parseObject(s, Map.class);
        String signContent = getSignContent(map);
        System.out.println(signContent);
        System.out.println(signContent.equals(s3));
    }

    /**
     * @param params map参数
     * @return 待签名字符串
     */
    public static String getSignContent(Map<String, String> params) {
        StringBuilder content = new StringBuilder();
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        int index = 0;
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = String.valueOf(params.get(key));
            // key或value为空不参与签名
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value) && !"null".equals(value)
                    && !"signMsg".equals(key)) {
                content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
                index++;
            }
        }
        return content.toString();
    }

}
